using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CPlatePrintSys.DataAccess.Context;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Infrastructure.Repositories;

namespace CPlatePrintSys.DataAccess.Repositories
{
    public class GlobalConfigRepository : IGlobalConfigRepository
    {
        private readonly CPlateDbContext _context;

        public GlobalConfigRepository(CPlateDbContext context)
        {
            _context = context;
        }

        public async Task<GlobalConfig> GetByIdAsync(object id)
        {
            return await _context.GlobalConfigs.FindAsync(id);
        }

        public async Task<IEnumerable<GlobalConfig>> GetAllAsync()
        {
            return await _context.GlobalConfigs.ToListAsync();
        }

        public async Task<GlobalConfig> AddAsync(GlobalConfig entity)
        {
            entity.UpdatedAt = DateTime.Now;
            _context.GlobalConfigs.Add(entity);
            await _context.SaveChangesAsync();
            return entity;
        }

        public async Task UpdateAsync(GlobalConfig entity)
        {
            var dbEntity = await _context.GlobalConfigs.FindAsync(entity.Id);
            if (dbEntity == null)
                throw new Exception("配置不存在");

            dbEntity.ConfigValue = entity.ConfigValue;
            dbEntity.Description = entity.Description;
            dbEntity.UpdatedAt = DateTime.Now;
            dbEntity.UpdatedBy = entity.UpdatedBy;

            await _context.SaveChangesAsync();
        }

        public async Task DeleteAsync(object id)
        {
            var entity = await _context.GlobalConfigs.FindAsync(id);
            if (entity != null)
            {
                _context.GlobalConfigs.Remove(entity);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<GlobalConfig>> FindAsync(Expression<Func<GlobalConfig, bool>> predicate)
        {
            return await _context.GlobalConfigs.Where(predicate).ToListAsync();
        }

        public async Task<int> CountAsync(Expression<Func<GlobalConfig, bool>> predicate = null)
        {
            if (predicate == null)
                return await _context.GlobalConfigs.CountAsync();
            return await _context.GlobalConfigs.CountAsync(predicate);
        }

        public async Task<GlobalConfig> GetByKeyAsync(string configKey)
        {
            return await _context.GlobalConfigs.FirstOrDefaultAsync(c => c.ConfigKey == configKey);
        }

        public async Task<string> GetValueByKeyAsync(string configKey)
        {
            var config = await _context.GlobalConfigs.FirstOrDefaultAsync(c => c.ConfigKey == configKey);
            return config?.ConfigValue;
        }

        public async Task UpdateValueAsync(string configKey, string configValue, string updatedBy)
        {
            var config = await _context.GlobalConfigs.FirstOrDefaultAsync(c => c.ConfigKey == configKey);
            if (config == null)
            {
                // 如果配置不存在，创建新的
                config = new GlobalConfig
                {
                    ConfigKey = configKey,
                    ConfigValue = configValue,
                    UpdatedBy = updatedBy,
                    UpdatedAt = DateTime.Now
                };
                _context.GlobalConfigs.Add(config);
            }
            else
            {
                // 更新现有配置
                config.ConfigValue = configValue;
                config.UpdatedBy = updatedBy;
                config.UpdatedAt = DateTime.Now;
            }

            await _context.SaveChangesAsync();
        }

        public async Task<bool> ExistsAsync(string configKey)
        {
            return await _context.GlobalConfigs.AnyAsync(c => c.ConfigKey == configKey);
        }

        public async Task<List<string>> GetAllKeysAsync()
        {
            return await _context.GlobalConfigs.Select(c => c.ConfigKey).ToListAsync();
        }
    }
} 